#include <iostream>
#include <unistd.h>
 #include <sched.h>
#include <unistd.h>
#include <sys/wait.h>

#define STACK_SIZE  (1024 * 1024)

 int child_func(void *arg)
{
    while(true)
    {
        printf("Child process: PID = %d\n", getpid());
        sleep(5);
    }
    return 0;
}

int main()
{
    char* stack = (char*)malloc(STACK_SIZE);
    if(stack == NULL)
    {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    //使用 clone 创建子进程
    pid_t pid = clone(child_func, stack + STACK_SIZE, CLONE_VM | SIGCHLD, NULL);
    if(pid == -1)
    {
        perror("clone");
        free(stack);
        exit(EXIT_FAILURE);
    }
    printf("Parent process:PID = %d, Child PID = %d\n", getpid(), pid);
    if(waitpid(pid, NULL, 0) == -1)
    {
        perror("waitpid");
        free(stack);
        exit(EXIT_FAILURE);
    }
    free(stack);
    return 0;
}